import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import mutations from './mutations'
import getters from './getters'
const syncStorageCart = store => {
  store.subscribe((mutation, state) => {
    window.localStorage.setItem('add-cart', JSON.stringify(state.cart))
  })
}
const syncUser = store => {
  store.subscribe((mutation, state) => {
    window.localStorage.setItem('login-user', JSON.stringify(state.user))
  })
}
Vue.use(Vuex)
export default new Vuex.Store(
  {
    strict: true, // 严格模式,state可以看成是vue中的data
    state,
    // getters: {
    //   namelength (state) {
    //     return state.name.length
    //   }

    // },
    mutations,
    getters,
    plugins: [syncStorageCart, syncUser],
    actions: {
      incAsync (store, n) {
        setTimeout(() => {
          store.commit('add', n)
        }, 3000)
      }
    }

  }
)

// 关于用法怎么用router就怎么用vuex
